import api from '../../api';
import { message } from 'antd';

export default {
  namespace: 'optimizationM',
  state: {},
  effects: {
    // 查询持仓设置列表-真实
    *queryHoldRealData({ payload, callback }, { call, put }) {
      const res = yield call(api.workStation.stockTable, payload);
      if (res?.success) {
        callback(res.data ?? []);
      } else {
        callback();
        message.error(res?.message || '查询持仓设置列表失败！');
      }
    },
    // 查询持仓设置列表-虚拟
    *queryHoldVirtualData({ payload, callback }, { call, put }) {
      const res = yield call(api.workStation.queryHoldData, payload);
      if (res?.code == '0') {
        let data = res?.data?.dataSource || [];
        callback(data);
      } else {
        callback();
        message.error(res?.msg || '查询持仓设置列表失败！');
      }
    },
    //查询风险因子约束表格
    *queryRisk({ payload, callback }, { call, put }) {
      const response = yield call(api.portfolioOptimization1.queryRisk1, payload);
      if (response && response.code == 0) {
        callback(response.data);
      } else {
        callback();
        message.error(response?.msg || '查询风险因子约束失败!');
      }
    },
    //执行分析按钮 请求分析数据 股票
    *executeOptimization({ payload, callback }, { call, put }) {
      const response = yield call(api.portfolioOptimization1.executeOptimization1, payload);
      if (response && response.code == 0) {
        callback(response.data || '1');
      } else {
        message.error(response?.msg || '提交失败!');
      }
    },
    //一键调仓
    *changeMyPosition({ payload, callback }, { call, put }) {
      const response = yield call(api.workStation.changePosition, payload);
      if (response && response.code == 0) {
        message.success('一键调仓成功!');
        callback();
      } else {
        message.error(response?.msg || '一键调仓失败!');
      }
    },
  },

  reducers: {
    setState(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },
};
